const { dialog, ipcMain } = require('electron');
const { getMainWindow } = require('./windowManager');

const path = require('node:path');

function browseFiles() {
  ipcMain.handle('browse-files', async (_event, _) => {
    const mainWindow = getMainWindow(); // 从共享状态模块中获取 mainWindow 
    const result = await dialog.showOpenDialog(mainWindow, {
      properties: ['openFile', 'openDirectory', 'multiSelections'], // 支持文件夹、文件和多选
      filters: [
        { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, // 文件类型过滤
        { name: 'All Files', extensions: ['*'] },
      ],
    });
    if (result.canceled || result.filePaths.length === 0) {
      return null; // 用户取消操作
    }
  
    let path  = result.filePaths[0].toString();
    return path; // 返回选中的文件路径
  });
}

module.exports = { browseFiles };